import 'package:base_plugin/base_plugin.dart';
import 'package:collection/collection.dart';
import 'package:finance_app/src/widgets/bottom_sheet/filter_bottom_sheet.dart';

mixin FilterTransactionSortMixin<T> on BaseProvider<T> {
  List<String> currentSelectFilterList = [];

  String get rangeTime;

  int get filterListLength =>
      currentSelectFilterList.length + (rangeTime != null ? 1 : 0);

  Map<String, String> filterToKey = {
    'Quotation': 'quotation',
    'Purchase Order': 'purchase_order',
    'Invoice': 'invoice',
    'Bill': 'bill',
    'Money-In': 'cash_in',
    'Expense': 'expense',
    'Money-Out': 'cash_out',
    'Unpaid Bill': 'bill',
    'Paid Bill': 'bill',
    'Overdue Bill': 'bill',
    'Pending Expense': 'expense',
    'Unpaid Expense': 'expense',
    'Overdue Expense': 'expense',
    'Paid Expense': 'expense',
    'Unpaid Invoice': 'invoice',
    'Credit Note': 'credit_note',
    'Debit Note': 'debit_note',
  };

  Map<String, String> statusToKey = {
    'Open': 'open',
    'Closed': 'closed',
    'Accepted': 'accepted',
    'Rejected': 'rejected',
    'Unpaid': 'open',
    'Paid': 'closed',
    'Open PO': 'open',
    'Billed PO': 'billed',
    'Closed PO': 'closed',
    'Unpaid Bill': 'open',
    'Paid Bill': 'closed',
    'Fully Credited': 'closed',
    'Credit Available': 'open',
    'Unpaid Invoice': 'open',
    'Overdue': 'open',
    'Overdue Bill': 'open',
    'Pending Expense': 'open',
    'Unpaid Expense': 'open',
    'Overdue Expense': 'open',
    'Paid Expense': 'closed',
  };

  List get filterList => null;

  void onRequestRefresh();

  void unFocus();

  void onShowFilter() {
    unFocus();
    obtainContext.call((context) async {
      List<String> result = await showFilterTransactionBottomSheet(
        context,
        selectFilter: List.from(currentSelectFilterList),
        defaultFilter: filterList,
      );
      if (!DeepCollectionEquality().equals(result, currentSelectFilterList)) {
        currentSelectFilterList = result;
        update();
        onRequestRefresh();
      }
    });
  }

  void onRemoveFilter(String value) {
    currentSelectFilterList.remove(value);
    update();
    onRequestRefresh();
  }
}
